<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org">
<head>
    <th:block th:include="include :: header('修改课件')"/>
    <th:block th:include="include :: bootstrap-fileinput-css"/>
</head>
<body class="white-bg">
<div class="wrapper wrapper-content animated fadeInRight ibox-content">
    <form class="form-horizontal m" id="form-courseware-edit" th:object="${ccCourseware}">
        <input name="id" th:field="*{id}" type="hidden">
        <div class="form-group">
            <label class="col-sm-3 control-label">课件名称：</label>
            <div class="col-sm-8">
                <input name="coursewareName" th:field="*{coursewareName}" class="form-control" type="text">
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">备注：</label>
            <div class="col-sm-8">
                <textarea name="remark" class="form-control">[[*{remark}]]</textarea>
            </div>
        </div>
        <div class="form-group">
            <label class="col-sm-3 control-label">课件：</label>
            <div class="col-sm-8">
                <input type="hidden" name="coursewareUrl" th:field="*{coursewareUrl}">
                <div class="file-loading">
                    <input class="form-control file-upload" id="coursewareUrl" name="file" type="file">
                </div>
            </div>
        </div>

    </form>
</div>
<th:block th:include="include :: footer"/>
<th:block th:include="include :: bootstrap-fileinput-js"/>
<script th:inline="javascript">
    var prefix = ctx + "courseware/courseware";
    $("#form-courseware-edit").validate({
        focusCleanup: true
    });

    function submitHandler() {
        if ($.validate.form()) {
            edit()
        }
    }

    function edit() {
        var formData = new FormData();
        formData.append("id", $("[name=id]").val())
        formData.append("coursewareName", $("[name=coursewareName]").val())
        formData.append("remark", $("[name=remark]").val())
        if ($('#coursewareUrl[name=file]')[0].files[0] != undefined) {
            formData.append('coursewareFile', $('#coursewareUrl[name=file]')[0].files[0]);
        }
        $.ajax({
            url: prefix + "/add",
            type: 'post',
            cache: false,
            data: formData,
            processData: false,
            contentType: false,
            dataType: "json",
            success: function (result) {
                $.operate.successCallback(result);
            }
        });
    }

    $(".file-upload").each(function (i) {
        var val = $("input[name='" + this.id + "']").val()
        $(this).fileinput({
            initialPreviewAsData: true,
            initialPreview: [val],
            maxFileCount: 1,
            autoReplace: true,
            showUpload: false,
            showPreview: false,//是否预览
            dropZoneTitle: "拖拽文件到这里 …" + `<br>` + "仅支持单文件上传",
        }).on('fileuploaded', function (event, data, previewId, index) {
            $("input[name='" + event.currentTarget.id + "']").val(data.response.url)
        }).on('fileremoved', function (event, id, index) {
            $("input[name='" + event.currentTarget.id + "']").val('')
        })
        $(this).fileinput('_initFileActions');
    });
</script>
</body>
</html>